/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.servlets;

import com.oea.hibernate.entities.Answer;
import com.oea.hibernate.entities.Result;
import com.oea.hibernate.entities.Student;
import com.oea.hibernate.entities.Test_Question;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author unnati
 */
public class TestLogicAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String path="start-test.jsp";
        try (PrintWriter out = response.getWriter())
        {
            String action = request.getParameter("action");

            String qindex_str = (String) request.getSession().getAttribute("qindex");
            int qindex = Integer.parseInt(qindex_str);
            List<Test_Question> test_questions = (List<Test_Question>) request.getSession().getAttribute("allquestions");
            
            int total = test_questions.size();

            String uans = request.getParameter("uans");
            request.getSession().setAttribute("uanswer", uans);
           System.out.println("hi" + uans);
            if (action.equals("Previous")) 
            {System.out.println(qindex);

                test_questions.get(qindex).getQuestion().setUans(uans);

                if (qindex > 0) 
                {
                    qindex--;

                } 
                else
                {
                    qindex = total - 1;
                }
                System.out.println("last"+qindex);
                request.getSession().setAttribute("qindex", qindex + "");
                request.getSession().setAttribute("question", test_questions.get(qindex).getQuestion());
               

            }
            else if (action.equals("Next")) 
            {
                System.out.println(qindex);
                test_questions.get(qindex).getQuestion().setUans(uans);

                if (qindex < (total - 1))
                {
                    qindex++;

                } else
                {
                    qindex = 0;
                }
                request.getSession().setAttribute("qindex", qindex + "");
                request.getSession().setAttribute("question", test_questions.get(qindex).getQuestion());

            } 
            else if (action.equals("Submit")) 
            {

                int correct = 0;
                int wrong = 0;

                for (Test_Question t : test_questions)
                {
                    Student s=(Student) request.getSession().getAttribute("student");
                    
                    
                    Answer ans = new Answer();
                    ans.setTid(t.getTid());
                    ans.setQid(t.getQid());
                    ans.setSid(s.getId());
                    ans.setSanswer(t.getQuestion().getUans());
                    request.getSession().setAttribute("tota_que", t.getTest().getTotalquestions());
//                   request.getSession().setAttribute("positive_marking", t.getTest().getPositivemarking());
//                   request.getSession().setAttribute("negative_marking", t.getTest().getNegativemarking());
                   

                    ans = (Answer) ans.add();                    
                    
                    if(t.getQuestion().getAnswer().equalsIgnoreCase(t.getQuestion().getUans())){
                        correct++;
                    }
                    else
                    {
                        wrong++;
                    }
                    // put corrent and wrong in session
                   
                   
//                   request.getSession().setAttribute("total_marks", total_marks);
                   

                }
//                 Test_Question t=new Test_Question();
//                System.out.println(t.getTest().getTotalquestions());
                Student student = (Student) request.getSession().getAttribute("student");
                Long stu_id = student.getId();
                
                Long testid = (Long) request.getSession().getAttribute("testid");
                
                int skipans = Answer.GetNullAns(testid, stu_id).size();
                System.out.println(skipans);
                System.out.println("ronak");
                
                wrong=wrong-skipans;
                
                request.getSession().setAttribute("correct_Ans", correct);
                request.getSession().setAttribute("wrong_Ans", wrong);
                
                float positive_marks = (correct) * (test_questions.get(0).getTest().getPositivemarking());
                float negative_marks = (wrong) * (test_questions.get(0).getTest().getNegativemarking());
                float total_marks = positive_marks - negative_marks;
                
                int cutoffmarks = (test_questions.get(0).getTest().getCutoffmarks());
                boolean status = false;
                
                if(cutoffmarks<total_marks)
                {
                    status=true;
                }
                Result r=new Result();
                r.setSid(stu_id);
                r.setStatus(status);
                r.setCanswer(correct);
                r.setWanswer(wrong);
                r.setTid(testid);
                r.setSkipans(skipans);
                r.add();
                if(status)
                {
                    String result="Pass";
                    request.getSession().setAttribute("status", result);
                }
                else
                {
                     String result="Fail";
                    request.getSession().setAttribute("status", result);
                }
                
                path="student-result.jsp";
            } 
            else if (action.equals("getQuestion"))
            {
               
                int btn_index = Integer.parseInt(request.getParameter("qindex"));
                System.out.println(btn_index);
       // int b=btn_index-1;
                if(btn_index==0)
                {
                    test_questions.get(btn_index+1).getQuestion().setUans(uans);
                }
                else
                {
                test_questions.get(btn_index-1).getQuestion().setUans(uans);
                }
               // System.out.println(btn_index);
              
                request.getSession().setAttribute("qindex", btn_index + "");
                request.getSession().setAttribute("question", test_questions.get(btn_index).getQuestion());
            }
            request.getSession().setAttribute("allquestions", test_questions);
            response.sendRedirect(path);

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
